package com.example.withroadlogistics.weibo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

import com.example.withroadlogistics.Adapter.Adapter_Weibo;
import com.example.withroadlogistics.R;
import com.example.withroadlogistics.model.VBlogUser;
import com.example.withroadlogistics.until.MySharedPreferences;
import com.example.withroadlogistics.until.constant;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class workweibo extends AppCompatActivity {

    //获取用户发表的微博列表
    private ListView mListView;
    private List<VBlogUser> mList = new ArrayList<>();
    //访问本地接口，用10.0.2.2取代localhost
    String url = constant.url+"blog/getbloglist";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_workweibo);
        mListView = findViewById(R.id.mListView);//将mlistvie控件实例
    }
    public void onWindowFocusChanged(boolean hasFocus)
    {
        /**
         * 请求数据
         * 解析json
         * 适配器
         * 绑值显示
         */
        if (hasFocus){

            RxVolley.get(url, new HttpCallback() {
                @Override
                public void onSuccess(String t) {
                    // Toast.makeText(weibosend.this,t,Toast.LENGTH_LONG).show();

                    //控制台输出Json
                    //  Log.d("aaaa",""+t);

                    //解析json
                    parsingJson(t);

                    Adapter_Weibo adapter = new Adapter_Weibo(workweibo.this,mList);

                    mListView.setAdapter(adapter);


                }
            });
        }
    }

    //数据解析
    public void parsingJson(String t){
        try {

            JSONObject jsonObject = new JSONObject(t);
            JSONArray jsonList = jsonObject.getJSONArray("data");
            for(int i=0;i<jsonList.length();i++){


                JSONObject json = jsonList.getJSONObject(i);
                int id=Integer.parseInt(json.getString("id"));
                String userid=json.getString("userid");
                String contents=json.getString("contents");
                String releasetime=json.getString("releasetime");
                String blogid=json.getString("blogid");
                String blognote=json.getString("blognote");
                String username=json.getString("username");
                String realname=json.getString("realname");
                int roleid=Integer.parseInt(json.getString("roleid"));
                String pwd=json.getString("pwd");
                String phone=json.getString("phone");
                int status=Integer.parseInt(json.getString("status"));
                int photoid=Integer.parseInt(json.getString("photoid"));
                String lastlogindate=json.getString("lastlogindate");
                String createtime=json.getString("createtime");
                String loginstatus=json.getString("loginstatus");

                //要将model定义的成员全部写出
                mList.add(new VBlogUser(id,userid,contents,releasetime,blogid,blognote,username,realname,roleid,pwd,phone,status,photoid,lastlogindate,createtime,loginstatus));

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

}
